package com.tianya.portal.common.utils;

import com.tianya.portal.common.utils.markdown.CodeAttributeProvider;
import org.commonmark.node.Node;
import org.commonmark.parser.Parser;
import org.commonmark.renderer.html.AttributeProvider;
import org.commonmark.renderer.html.AttributeProviderContext;
import org.commonmark.renderer.html.AttributeProviderFactory;
import org.commonmark.renderer.html.HtmlRenderer;

/**
 * MarkDown工具类
 */
public class MarkDownUtils {

    public static String getRendererHtml(String text) {
        Parser parser = Parser.builder().build();
        Node document = parser.parse(text);
        HtmlRenderer renderer = HtmlRenderer.builder().attributeProviderFactory(new AttributeProviderFactory() {
            public AttributeProvider create(AttributeProviderContext context) {
                return new CodeAttributeProvider();
            }
        }).build();
        String html = renderer.render(document);
        return html;
    }
}
